package com.wuwei.aliocr.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.aliocr.domain.LessonRecord;
import com.wuwei.aliocr.mapper.LessonRecordMapper;
import com.wuwei.aliocr.utils.CommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class LessonRecordDao {
    @Resource
    private LessonRecordMapper lessonRecordMapper;

    @Resource
    private CommonUtils commonUtils;

    public void save(List<LessonRecord> list) {
        //将9月12日转换成xxxx年-xx月-xx日的格式
        Integer year = commonUtils.nowYear();
        String stringYear = String.valueOf(year);
        // 如果是mybatis,尽量别直接调用多次insert,自己写一个mapper里面新增一个方法batchInsert,所有数据一次性插入
        LessonRecord firstLessonRecord = list.get(0);
        String firstWorkdayDate = commonUtils.mySqlDateFormatDateDashToString(firstLessonRecord.getDate());
        String toFormatDay = stringYear + "年" + firstWorkdayDate;
        String formattedFirstWorkdayDate = commonUtils.dateFormat(toFormatDay);
        LessonRecord lessonRecord2 = new LessonRecord();
        BeanUtils.copyProperties(firstLessonRecord, lessonRecord2);
        lessonRecord2.setDate(commonUtils.mySqlDateFormatStringOfDashToDate(formattedFirstWorkdayDate));
        String workday2Subject = lessonRecord2.getSubject();
        if (!("/").equals(workday2Subject) &&!"自习".equals(workday2Subject)&&null!=workday2Subject) {
            this.insertSql(lessonRecord2);
        }
        for (int i = 1; i < list.size(); i++) {
            LessonRecord lessonRecord = list.get(i);
            if (i<list.size()-1){
                //查找本元素的date和weekend是否有值，如果没有值，则使用上一个元素的值
                if (lessonRecord.getDate() == null) {
                    lessonRecord.setDate(list.get(i-1).getDate());
                    lessonRecord.setWeekend(list.get(i-1).getWeekend());
                }
                if (lessonRecord.getMorningOrAfternoon() == null) {
                    lessonRecord.setMorningOrAfternoon(list.get(i-1).getMorningOrAfternoon());
                }
            }else {
                //查找上一个元素的值赋给他
                lessonRecord.setDate(list.get(i-1).getDate());
                lessonRecord.setMorningOrAfternoon(list.get(i-1).getMorningOrAfternoon());
                lessonRecord.setWeekend(list.get(i-1).getWeekend());
            }
            //这里需要将workday里日期的格式转换一下
            String workdayDate = commonUtils.mySqlDateFormatDateDashToString(lessonRecord.getDate());
            String dateFormattedDate = commonUtils.dateFormat(stringYear + "年" + workdayDate);
            LessonRecord lessonRecord1 = new LessonRecord();
            BeanUtils.copyProperties(lessonRecord, lessonRecord1);
            lessonRecord1.setDate(commonUtils.mySqlDateFormatStringOfDashToDate(dateFormattedDate));
            String workday1Subject = lessonRecord1.getSubject();
            if (!("/").equals(workday1Subject) &&!"自习".equals(workday1Subject)&&null!=workday1Subject) {
                this.insertSql(lessonRecord1);
            }
        }
    }

    void insertSql( LessonRecord lessonRecord){
        //判断数据库中有无该数据库，如果有则不同步
        LambdaQueryWrapper<LessonRecord> queryWrapper = new LambdaQueryWrapper<>();
        Date date1 = lessonRecord.getDate();
        String date = commonUtils.mySqlDateFormatDateDashToString(date1);
        String time = lessonRecord.getTime();
        String subject = lessonRecord.getSubject();
        String teacher = lessonRecord.getTeacher();
        queryWrapper.eq(date!=null, LessonRecord::getDate,date);
        queryWrapper.eq(time!=null, LessonRecord::getTime,time);
        queryWrapper.eq(subject!=null, LessonRecord::getSubject,subject);
        queryWrapper.eq(teacher!=null, LessonRecord::getTeacher,teacher);
        LessonRecord selectedLessonRecord = lessonRecordMapper.selectOne(queryWrapper);
        if (selectedLessonRecord == null) {
            lessonRecordMapper.insert(lessonRecord);
        }
    }
}
